#include "base/types.h"
#include "base/result_codes.h"

#include "engine/result_codes.h"
#include "engine/dator.h"

#include "cppunit/TestFixture.h"
#include "cppunit/extensions/HelperMacros.h"

using namespace engine;

class DatorTest : public CppUnit::TestFixture {
    CPPUNIT_TEST_SUITE(DatorTest);
    CPPUNIT_TEST(testSet);
    CPPUNIT_TEST(testToString);
    CPPUNIT_TEST_SUITE_END();

public:
    void setUp() { }
    void tearDown() { }

    void testSet() {
        Dator * dator;
        char char_ = ' ';
        //int8 int8_ = 0;
        int16 int16_ = 0;
        int32 int32_ = 0;
        int64 int64_ = 0;
        short short_ = 0;
        long long_ = 0;
        long long long_long_ = 0;
        bool bool_ = false;
        float float_ = 0.0f;
        double double_ = 0.0;
        std::string string_ = "";

        //dator = new Setting<int8>(int8_);
        //CPPUNIT_ASSERT_EQUAL(base::OK_OK, dator->Set("5"));
        //CPPUNIT_ASSERT(int8_ == 5);
        //SAFE_DELETE( dator );

        //dator = new Setting<int8>(int8_);
        //CPPUNIT_ASSERT_EQUAL(base::OK_OK, dator->Set("-5"));
        //CPPUNIT_ASSERT(int8_ == -5);
        //SAFE_DELETE( dator );


        dator = new Setting<int16>(int16_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("299"), base::OK_OK));
        CPPUNIT_ASSERT(int16_ == 299);
        SAFE_DELETE( dator );

        dator = new Setting<int16>(int16_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-299"), base::OK_OK));
        CPPUNIT_ASSERT(int16_ == -299);
        SAFE_DELETE( dator );

        dator = new Setting<int32>(int32_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("34445"), base::OK_OK));
        CPPUNIT_ASSERT(int32_ == 34445);
        SAFE_DELETE( dator );

        dator = new Setting<int32>(int32_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-34445"), base::OK_OK));
        CPPUNIT_ASSERT(int32_ == -34445);
        SAFE_DELETE( dator );

        dator = new Setting<char>(char_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("r"), base::OK_OK));
        CPPUNIT_ASSERT(char_ == 'r');
        SAFE_DELETE( dator );

        
        dator = new Setting<short>(short_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("34"), base::OK_OK));
        CPPUNIT_ASSERT(short_ == 34);
        SAFE_DELETE( dator );
        
        dator = new Setting<short>(short_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-34"), base::OK_OK));
        CPPUNIT_ASSERT(short_ == -34);
        SAFE_DELETE( dator );

        dator = new Setting<long>(long_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("34555"), base::OK_OK));
        CPPUNIT_ASSERT(long_ == 34555);
        SAFE_DELETE( dator );

        dator = new Setting<long>(long_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-34555"), base::OK_OK));
        CPPUNIT_ASSERT(long_ == -34555);
        SAFE_DELETE( dator );

        dator = new Setting<long long>(long_long_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("33445566"), base::OK_OK));
        CPPUNIT_ASSERT(long_long_ == 33445566);
        SAFE_DELETE( dator );

        dator = new Setting<long long>(long_long_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-33445566"), base::OK_OK));
        CPPUNIT_ASSERT(long_long_ == -33445566);
        SAFE_DELETE( dator );

        dator = new Setting<float>(float_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("3344.5566"), base::OK_OK));
        CPPUNIT_ASSERT(float_ == 3344.5566f);
        SAFE_DELETE( dator );

        dator = new Setting<float>(float_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-3344.5566"), base::OK_OK));
        CPPUNIT_ASSERT(float_ == -3344.5566f);
        SAFE_DELETE( dator );

        dator = new Setting<double>(double_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("334455.66"), base::OK_OK));
        CPPUNIT_ASSERT(double_ == 334455.66);
        SAFE_DELETE( dator );

        dator = new Setting<double>(double_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("-334455.66"), base::OK_OK));
        CPPUNIT_ASSERT(double_ == -334455.66);
        SAFE_DELETE( dator );

        dator = new Setting<bool>(bool_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("true"), base::OK_OK));
        CPPUNIT_ASSERT(bool_ == true);
        SAFE_DELETE( dator );

        dator = new Setting<bool>(bool_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("false"), base::OK_OK));
        CPPUNIT_ASSERT(bool_ == false);
        SAFE_DELETE( dator );

        dator = new StringSetting(string_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("moo cow"), base::OK_OK));
        CPPUNIT_ASSERT(string_ == "moo cow");
        SAFE_DELETE( dator );

        dator = new StringSetting(string_);
        CPPUNIT_ASSERT(result_code::Equals(dator->Set("a really ridiculously long test string omg it just keeps going!"), base::OK_OK));
        CPPUNIT_ASSERT(string_ == "a really ridiculously long test string omg it just keeps going!");
        SAFE_DELETE( dator );
    }

    void testToString() {
        Dator * dator;
        std::string output;

        char char_;
        //int8 int8_; 
        int16 int16_;
        int32 int32_;
        int64 int64_;
        short short_;
        long long_; 
        long long long_long_;
        bool bool_; 
        float float_; 
        double double_; 
        std::string string_;

        //dator = new Setting<int8>(int8_);
        //int8_ = 5;
        //CPPUNIT_ASSERT_EQUAL(base::OK_OK, dator->ToString(output));
        //CPPUNIT_ASSERT(output == "5");
        //SAFE_DELETE( dator );

        //dator = new Setting<int8>(int8_);
        //int8_ = -5;
        //CPPUNIT_ASSERT_EQUAL(base::OK_OK, dator->ToString(output));
        //CPPUNIT_ASSERT(output == "-5");
        //SAFE_DELETE( dator );

        dator = new Setting<int16>(int16_);
        int16_ = 299;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "299");
        SAFE_DELETE( dator );

        dator = new Setting<int16>(int16_);
        int16_ = -299;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-299");
        SAFE_DELETE( dator );

        dator = new Setting<int32>(int32_);
        int32_ = 34445;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "34445");
        SAFE_DELETE( dator );

        dator = new Setting<int32>(int32_);
        int32_ = -34445;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-34445");
        SAFE_DELETE( dator );

        dator = new Setting<char>(char_);
        char_ = 'r';
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "r");
        SAFE_DELETE( dator );

        
        dator = new Setting<short>(short_);
        short_ = 34;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "34");
        SAFE_DELETE( dator );
        
        dator = new Setting<short>(short_);
        short_ = -34;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-34");
        SAFE_DELETE( dator );

        dator = new Setting<long>(long_);
        long_ = 34555;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "34555");
        SAFE_DELETE( dator );

        dator = new Setting<long>(long_);
        long_ = -34555;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-34555");
        SAFE_DELETE( dator );

        dator = new Setting<long long>(long_long_);
        long_long_ = 33445566;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "33445566");
        SAFE_DELETE( dator );

        dator = new Setting<long long>(long_long_);
        long_long_ = -33445566;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-33445566");
        SAFE_DELETE( dator );

        dator = new Setting<int64> (int64_);
        int64_ = 66554433;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "66554433");
        SAFE_DELETE( dator );

        dator = new Setting<int64>(int64_);
        int64_ = -66554433;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "-66554433");
        SAFE_DELETE( dator );

        dator = new Setting<float>(float_);
        float_ = 3344.5566f;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));       
        CPPUNIT_ASSERT_EQUAL((float)atof(output.c_str()), float_);
        SAFE_DELETE( dator );

        dator = new Setting<float>(float_);
        float_ = -3344.5566f;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT_EQUAL((float)atof(output.c_str()), float_);
        SAFE_DELETE( dator );

        dator = new Setting<double>(double_);
        double_ = 334455.66;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT_EQUAL(atof(output.c_str()), double_);
        SAFE_DELETE( dator );

        dator = new Setting<double>(double_);
        double_ = -334455.66;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT_EQUAL(atof(output.c_str()), double_);
        SAFE_DELETE( dator );

        dator = new Setting<bool>(bool_);
        bool_ = true;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "true");
        SAFE_DELETE( dator );

        dator = new Setting<bool>(bool_);
        bool_ = false;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "false");
        SAFE_DELETE( dator );

        dator = new StringSetting(string_);
        string_ = "moo cow" ;
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "moo cow");
        SAFE_DELETE( dator );

        dator = new StringSetting(string_);
        string_ = "a really ridiculously long test string omg it just keeps going!";
        CPPUNIT_ASSERT(result_code::Equals(dator->ToString(output), base::OK_OK));
        CPPUNIT_ASSERT(output == "a really ridiculously long test string omg it just keeps going!");
        SAFE_DELETE( dator );
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(DatorTest);